/* 
 * 自动完成插件
 */
define(function(require)
{
  var $ = require('suggest');
  var jxKit = require('jx_kit');
  require("json2") ;
	var $jac = 
	{
		initAll:function()
		{
			$("[jac_url]").each(function()
			{
				$jac.init(this) ;
			});
		},
		init:function(obj)
		{
			var curObject = obj ;
      $(curObject).bsSuggest('init',
      { 
        getDataMethod:"url", 
        url:$(curObject).attr("jac_url"),
        showBtn: true, 
        showHeader:false,
        effectiveFields:["value"],
        idField: "id",
        keyField: "value",
        fnProcessData: function (json) 
        {
          var data = {value: []};
          data.value = json;
          data.defaults = 'jxsm';
          return data;
        },	
        fnAdjustAjaxParam: function(keyword, opts) 
        {
  //调整 ajax 请求参数方法，用于更多的请求配置需求。如对请求关键字作进一步处理、修改超时时间等
          var dataTerm = "term" ;
          if (undefined != $(curObject).attr("jac_term"))
            dataTerm = $(curObject).attr("jac_term") ;
          var postData = {} ;
          postData[dataTerm] = $(curObject).val() ;

          if (undefined != $(curObject).attr("jac_dyterm"))
          {
            var datainfo = $(curObject).attr("jac_dyterm").split(",");
            $.each(datainfo, function(index, val)
            {
              postData[val.replace('#', '')] = $(val).val();
            });
          }	
          return {timeout: 10000,data: postData};
        }
      })
      .on("onDataRequestSuccess",function(e,result)
      {
        if (jxKit.isExitsFunction("onDataRequestSuccess"))
          onDataRequestSuccess(curObject,result) ;
      })
      .on("onSetSelectValue",function(e,keyword)
      {
        if (jxKit.isExitsFunction("onSetSelectValue"))
          onSetSelectValue(curObject,result) ;        
        $(curObject).bsSuggest("hide");
        $("#" + $(curObject).attr("jac_id")).val(keyword.id);

        if (undefined != $(curObject).attr("jac_change"))
          eval($(curObject).attr("jac_change")) ;

        if (jxKit.isExitsFunction("jx_auto_select_func"))
          jx_auto_select_func(curObject,keyword) ;
      })
      .on("onUnsetSelectValue",function(e)
      {
        if (jxKit.isExitsFunction("onUnsetSelectValue"))
          onSetSelectValue(curObject) ;           
      });
		}
	};
	
  $(function()
  {
    $jac.initAll() ;
  });
	
	return $jac ;
})